/* **************************************************************
NAME: yemenboundstable.ado 
CREATED: 30 September 2021 
EDITED: 30 September 2021 
AUTHOR: callan.corcoran@kellogg.northwestern.edu
PURPOSE: Program to create standard tables for the Yemen project 
************************************************************** */

cap prog drop yemenboundstable

prog def yemenboundstable
syntax varlist [if/], 	/// Variables to be included in table 
	[filename(string)] 		/// File name (default is TableX)
	[foldername(string)]	/// Output folder name (default is Replication/Output)
	[title(string)] 		/// Table title 
	[footnote(string)] 		/// Footnote text 
	[baselinevals] 			/// Add baseline values of variables, when applicable
	[winsorize] 			/// Winsorize variables 
	[addlcontrols(varlist)] /// Add list of additional controls 
	[fixedeffects(varlist)] /// Set fixed effects (default is village) 
	[stderrors(string)] 	/// Set standard errors (default is robust)
	
// **************************************************************
// PREPARE TABLE INPUTS 	

loc varlist asset_tot_value_end ctotalpc_end ctotalhh_end inc_total_end inc_LS_end inc_nonLS_end fs_index_end savings_index_end percep_econ_end hous_sindex_end debt_index_end 

loc if "endline_survey==1"
loc filename "ATable12" 
loc title "Appendix Table 12: Adjusted Bounds for Treatment Effects on Key Welfare Outcomes"
loc winsorize winsorize 
loc baselinevals baselinevals
loc foldername "$output_paper"


// FOLDER NAME 
// 	Set default folder to Replication/Output if not specified 
if "`foldername'"=="" {
	loc foldername "${rep_output}" 
}

// FILE NAME 
// 	Set a default filename 
if "`filename'"=="" {
	loc filename "TableX"
}
if "`winsorize'"=="winsorize" {
	loc filename "`filename'_win"
}
if "`baselinevals'"=="baselinevals" {
	loc filename "`filename'_blv"
}
if "`addlcontrols'"!="" {
	loc filename "`filename'_ctl"
}

// WINSORIZE 
//	Edit varlist to include winsorized variable names if "winsorize" selected 
loc templist `varlist'
loc varlist // Clear varlist 
foreach var in `templist' {
	loc currvar = subinstr("`var'","_end","",.) // Cut out "_end" suffix 
	if "`winsorize'"=="winsorize" {
		loc varlist `varlist' `currvar'_win1 // Add "_win1" suffix if "winsorize" is selected
	}
	if "`winsorize'"=="" {
		loc varlist `varlist' `currvar'
	}
}

// ADDITIONAL CONTROLS 
loc templist `addlcontrols'
loc addlcontrols 
foreach var in `templist' {
    loc currvar = subinstr("`var'","_bsl","_win1_bsl",.) // Cut out "_end" suffix 
	if "`winsorize'"=="winsorize" {
		loc addlcontrols `addlcontrols' `currvar' m_`currvar' // Add "_win1" suffix if "winsorize" is selected
	}
	if "`winsorize'"=="" {
		loc varlist `addlcontrols' `var' m_`var'
	}
}

// FIXED EFFECTS 
// 	Set fixed effects to village, if not specified 
if "`fixedeffects'"=="" {
	loc fixedeffects village 
}

// STANDARD ERRORS 
//	Default standard errors to robust 
if "`stderrors'"=="" {
	loc stderrors robust 
}

// IF-STATEMENT 
if "`if'"!="" {
    loc ifif "if `if'"
	loc andif "& `if'"
} 

// FOOTNOTES 
// 	Add footnotes about baseline values and additional controls
if "`winsorize'"=="winsorize" {
	loc footnote "`footnote' Variables winsorized at 1%."
}
if "`baselinevals'"=="baselinevals" {
	loc footnote "`footnote' Controls for baseline value of dependent variable."
}
if "`addlcontrols'"!="" {
	loc footnote "`footnote' Controls for additional variables."
}

// **************************************************************
// CREATE TABLE FRAMEWORK 

// Clear estimates 
clear
eststo clear
estimates drop _all 

// Create blank table
set obs 10
gen x = 1
gen y = 1

// Count outcomes and create placeholder estimates 
loc columns: word count `varlist' 

forval i = 1/`columns' { 
	eststo col`i': qui reg x y
}

// Set table cell locals 
loc j = 1
// Add in locals for additional table rows (bounds)
	local lowerset "A" "C" "D" 
	local upperset "D" "C" "A" 
	
	foreach type in "lower" "upper" {
	foreach suffix in "``type'set'" {
	// A = Lee bounds 
	// B = Min / Max bounds (exclude)
	// C = 0.25 SD 
	// D = 0.1 SD
		loc `type'`suffix'coef = `j'			
		loc ++j
		loc `type'`suffix'se = `j'			
		loc ++j	
	}
	
	if "`type'"=="lower" { // Add in treatment after lower 
		loc treatcoef = `j'				// Cell first line (Starred coefficient)
		loc ++j
		loc treatse = `j'			// Cell second line (SE)
		loc ++j
	}
	}
	
// Continue with statistics 
loc contmean = `j'		// Control mean 
loc ++j
loc contsd = `j'		// Control standard deviation 
loc ++j
loc obs = `j'			// Observations 
loc ++j
loc blcontrols  = `j'	// Binary for baseline controls 
loc ++j
loc numzero = `j'		// Count with outcome == 0

loc stats "" 			// Added scalars to be filled
loc col_titles "" 		// Labels for columns vars to be filled

use "${rep_analysis}.dta", clear


// **************************************************************
// REPLACE MISSINGS WITH ZEROS

// ADDITIONAL CONTROLS 
// 	Replace missings with zeroes 
/*
foreach var in `addlcontrols' {
	replace `var' = 0 if missing(`var') // Replace to 0 if missing 
}
*/

// **************************************************************
// FILL TABLE CELLS 
//	Table cells include coefficients, standard errors, p-values 

loc i = 1 // Start a counter 

foreach y_var in `varlist' { // Loop through table variables 
	
	//***********************************************************
	// BASELINE CONTROLS 
	// 	Replace missings with zeroes 
	// 	Add "missing" dummies to controls 

	// Check whether baseline value exists 
	cap confirm variable `y_var'_bsl
	
	// If baseline doesn't exist... 
	if _rc { 
		loc proxyflag = 1 
		loc created_var = 1					
		gen `y_var'_bsl = 1 			// Create stand-in baseline var
		gen m_`y_var'_bsl = 1 			// Create stand-in baseline dummy var
		estadd loc stat`blcontrols' = "No" : col`i' // Note that baseline controls not used
	}
	
	// If baseline exists... 
	else if !_rc {
		loc proxyflag = 0 
		loc created_var = 0
		replace `y_var'_bsl = 0 if missing(`y_var'_bsl) // Replace baseline to 0 if missing 	
		if ("`baselinevals'"=="baselinevals") estadd loc stat`blcontrols' = "Yes" : col`i' // Note baseline controls used 
		else estadd loc stat`blcontrols' = "No" : col`i' // Note baseline controls not used 
	}
	
	if "`baselinevals'"=="baselinevals" local blvals `y_var'_bsl m_`y_var'_bsl // Add to baseline variables local 
	else local blvals // Empty
	
	//***********************************************************
	// RUN REGRESSION 
	
	// Add Bounds Variables 
		foreach letter in "C" "D" { // "B" Exclude min / max bounds 
			tempvar `y_var'_end_l`letter'
			tempvar `y_var'_end_h`letter'
			g ``y_var'_end_l`letter'' = `y_var'_end
			g ``y_var'_end_h`letter'' = `y_var'_end
			
			
			if "`letter'"=="B" {
				sum `y_var'_end if treatment==0 
				replace ``y_var'_end_l`letter'' = `r(max)' if missing(``y_var'_end_l`letter'') & treatment==0 // Replace control missings with control maximum 
				replace ``y_var'_end_h`letter'' = `r(min)' if missing(``y_var'_end_h`letter'') & treatment==0 // Replace control missings with control minimum 

				sum `y_var'_end if treatment==1
				replace ``y_var'_end_l`letter'' = `r(min)' if missing(``y_var'_end_l`letter'') & treatment==1 // Replace treatment missings with treatment minimum 
				replace ``y_var'_end_h`letter'' = `r(max)' if missing(``y_var'_end_h`letter'') & treatment==1 // Replace treatment missings with treatment maximum 
		
			}
			
			else {
				if "`letter'"=="C" { // 0.25 SD 
					loc level = 0.25
				}	
				
				if "`letter'"=="D" { // 0.1 SD 
					loc level = 0.1
				}	
				
				sum `y_var'_end if treatment==0
				replace ``y_var'_end_l`letter'' = `r(mean)' + (`level' * `r(sd)') if missing(``y_var'_end_l`letter'') & treatment==0 
				replace ``y_var'_end_h`letter'' = `r(mean)' - (`level' * `r(sd)') if missing(``y_var'_end_h`letter'') & treatment==0 

				sum `y_var'_end if treatment==1 
				replace ``y_var'_end_l`letter'' = `r(mean)' - (`level' * `r(sd)') if missing(``y_var'_end_l`letter'') & treatment==1
				replace ``y_var'_end_h`letter'' = `r(mean)' + (`level' * `r(sd)') if missing(``y_var'_end_h`letter'') & treatment==1
			}
		}
	
	// MAIN / DEFAULT REGRESSION 
	qui areg `y_var'_end treatment `blvals' `addlcontrols' `ifif', absorb(`fixedeffects') vce(`stderrors') 
	
	// Save results to matrix treat for later use
	mat def treat = r(table)
	
	//***********************************************************
	// SAVE STATISTICS
		
	// Lee Bounds			
	leebounds `y_var'_end treatment // Run Lee bounds  
	
	mat def A = r(table) 
				
	foreach type in "lower" "upper" {

	// Standard error
	loc se = el(A, rownumb(A,"se"),colnumb(A,"treatment:`type'"))
	estadd loc stat``type'Ase' = "(" + string(`se',"%9.2f") + ")" : col`i' // Add standard error
		
	// P value: to get stars
	local thisp = el(A,rownumb(A,"pvalue"),colnumb(A,"treatment:`type'"))
		if `thisp' < 0.01 {
			loc bstar "***"
		}
		else if `thisp' < 0.05 {
			loc bstar "**"
		}
		else if `thisp' < 0.1 {
			loc bstar "*"
		}
		else {
			local bstar ""
		}
	// Assign to coefficient
		loc coefficient = el(A,rownumb(A,"b"),colnumb(A,"treatment:`type'"))
		estadd loc stat``type'Acoef' = string(`coefficient',"%9.2f") + "`bstar'": col`i'

	}
	
	foreach letter in "C" "D" { // "B"
	// Lower Bound Regressions 
	qui areg ``y_var'_end_l`letter'' treatment `blvals' `controls' , absorb(`fixedeffects') vce(`stderrors')
	mat def lower`letter' = r(table)
	
	// Upper Bound Regressions 
	qui areg ``y_var'_end_h`letter'' treatment `blvals' `controls' , absorb(`fixedeffects') vce(`stderrors')
	mat def upper`letter' = r(table)

	} 
	
	// Add measure of % zero
		qui count if `y_var'_end == 0 & e(sample) == 1
		loc top = r(N)
		qui count if  e(sample) == 1 
		loc bot = r(N)
		estadd loc stat`numzero' = string(`top'/`bot',"%9.2f"): col`i'

		// Add treatment coefficient and standard error
		local groups "treat"
		foreach suffix in "C" "D" { // "A" "B" 
		foreach type in "lower" "upper" {
			loc groups "`groups' `type'`suffix'"
	}
	}
		
	foreach group in `groups' {
		
		// Standard error
		loc se = el(`group',rownumb(`group',"se"),colnumb(`group',"treatment"))
		estadd loc stat``group'se' = "(" + string(`se',"%9.2f") + ")" : col`i' // Add standard error
			
		// P value: to get stars
		local thisp = el(`group',rownumb(`group',"pvalue"),colnumb(`group',"treatment"))
			if `thisp' < 0.01 {
				loc bstar "***"
			}
			else if `thisp' < 0.05 {
				loc bstar "**"
			}
			else if `thisp' < 0.1 {
				loc bstar "*"
			}
			else {
				local bstar ""
			}

		// Assign to coefficient
			loc coefficient = el(`group',rownumb(`group',"b"),colnumb(`group',"treatment"))
			estadd loc stat``group'coef' = string(`coefficient',"%9.2f") + "`bstar'": col`i'

	}

	// Control group mean and SD
	qui sum `y_var'_end if treatment == 0  & e(sample) == 1
	estadd loc stat`contmean' = string(`r(mean)', "%9.2f"): col`i'
	estadd loc stat`contsd' = string(`r(sd)', "%9.2f") : col`i'
		
	// Total obs
	qui sum `y_var'_end if e(sample) == 1
	estadd loc stat`obs' = string(`r(N)', "%9.0f"): col`i'
	
	qui sum ``y_var'_end_lB' if e(sample) == 1
	estadd loc stat`obsB' = string(`r(N)', "%9.0f"): col`i'
	
	loc ++i
	foreach group in `groups' {
		mat drop `group'
	}
	mat drop A 
		
	// Row labels and update locals
	loc thisvarlabel: variable label `y_var'_end // Extracts label from row var
	local col_titles "`col_titles' "`thisvarlabel'" "		
	
}
// end foreach y_var
	
// Set stats to include in table
forv i = 1/`j' {
	loc stats "`stats' stat`i' "
}
// end forv i

	
//***********************************************************
// EXPORT TABLE 

cd "`foldername'" // Call output folder directory 

esttab col* using "`filename'.csv", title("`title'") cells(none) ///
	nonum mtitle(`col_titles') stats(`stats',labels("Lee Lower Bound" " "  "0.25 SD Lower Bound" " "  "0.1 SD Lower Bound" " "  "Unadjusted Treatment Effect Estimate" " "   ///
	"0.1 SD Upper Bound" " " "0.25 SD Upper Bound" " " "Lee Upper Bound" " " ///
		"Control Mean" "Control SD" "Observations (Total)" "Observations (Bounded Samples)" ///
	"Controls for Baseline Values" "Proportion of Obs Equal Zero" )) ///
	compress wrap replace
	
end 
